<?php 
    global $curUser;
    if($_POST['commit'] == "Upload Now") {
        if ((($_FILES["user"]["type"]["avatar"] == "image/gif")
            || ($_FILES["user"]["type"]["avatar"] == "image/jpeg")
            || ($_FILES["user"]["type"]["avatar"] == "image/jpg")
            || ($_FILES["user"]["type"]["avatar"] == "image/pjpeg")
            || ($_FILES["user"]["type"]["avatar"] == "image/x-png")
            || ($_FILES["user"]["type"]["avatar"] == "image/png"))
            && ($_FILES["user"]["size"]["avatar"] < 512000))
        {
            if ($_FILES["user"]["error"]["avatar"] > 0){
                  echo "Return Code: " . $_FILES["user"]["error"]["avatar"] . "<br>";
            }
            else{
                if (!file_exists("Images/Upload/" . $_FILES["user"]["name"]["avatar"])){
                    move_uploaded_file($_FILES["user"]["tmp_name"]["avatar"],
                    "Images/Upload/" . $_FILES["user"]["name"]["avatar"]);
                }
                $url = "Images/Upload/" . $_FILES["user"]["name"]["avatar"];
                $photo = Photo::withData($url, "", "");
                Photo::addPhoto($photo, true);
                echo '<script>parent.window.location.reload(true);</script>';
            }
        }else if(($_FILES["user"]["size"]["avatar"] >= 512000)){
            echo "<script>alert('File is too large! Please upload files up to 512kb.');</script>";
            echo '<script>parent.window.location.reload(true);</script>';
        }
    }
    if($_POST['commit'] == 'Delete'){
        Photo::deletePhoto($curUser->avatarid, true);
        echo '<script>parent.window.location.reload(true);</script>';
    }
?>
<script>
    function edit(){
        document.getElementById('edit_avatar_link').style.display = "none";
        document.getElementById('avatar-form').style.display = "inherit";
    }
    function cancel(){
        document.getElementById('avatar-form').style.display = "none";
        document.getElementById('edit_avatar_link').style.display = "inherit";
    }
    function file_upload(){
        document.getElementById('avatar-form').onsubmit = null;
        document.getElementById('avatar-form').submit();
    }
    function delete_avatar(){
        document.getElementById('delete-avatar-form').onsubmit = null;
        document.getElementById('delete-avatar-form').submit();
    }
</script>
<div class="secondary">
  <h3><span class="meta">Your</span> Avatar</h3>
  <div id="avatar-preview" class="group">
<form accept-charset="UTF-8" action="<?php echo $this->baseUrl; ?>Account" class="edit_user" onsubmit="return false;" id="delete-avatar-form" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="delete"><input name="authenticity_token" type="hidden" value="9c26/CY9n7HghzVay4MpBkrMdCyIgSl6eW/7gk9mc/g="></div><div data-picture="" data-alt="<?php $curUser->firstname." ".$curUser->lastname; ?>" data-class="photo">
  <div data-src="<?php if($curUser->avatarid == 0) echo $this->baseUrl."Images/avatar-default.gif"; else echo $this->baseUrl.$curUser->url;?>"></div>
  <div data-src="<?php if($curUser->avatarid == 0) echo $this->baseUrl."Images/avatar-default.gif"; else echo $this->baseUrl.$curUser->url;?>" data-media="(-webkit-min-device-pixel-ratio: 1.5),
       (min--moz-device-pixel-ratio: 1.5),
       (-o-min-device-pixel-ratio: 3/2),
       (min-device-pixel-ratio: 1.5),
       (min-resolution: 1.5dppx)"></div>
<img alt="<?php $curUser->firstname." ".$curUser->lastname; ?>" class="photo" src="<?php if($curUser->avatarid == 0) echo $this->baseUrl."Images/avatar-default.gif"; else { $photo = Photo::withId($curUser->avatarid); echo $this->baseUrl.$photo->url; }?>"></div> 
<input type="submit" value="Change Avatar" onclick="edit();" class="action edit-avatar" id="edit_avatar_link" style="<?php if($curUser->avatarid == 0) echo "display: none;"; else echo "display: inherit;";?>margin-right: 5px; margin-bottom: 5px; background-color: #df3e7b; color: white;">
<input alt="Delete" onclick="delete_avatar();" class="form-btn" id="delete-avatar-button" name="commit" style="<?php if($curUser->avatarid == 0) echo "display: none;"; else echo "display: inherit;";?>" type="submit" value="Delete">
</form>
<form onsubmit="return false;" style="<?php if($curUser->avatarid == "0") echo "display: inherit;"; else echo "display: none;";?>" accept-charset="UTF-8" action="<?php echo $this->baseUrl; ?>Account" class="group" enctype="multipart/form-data" id="avatar-form" method="post">
    <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value="9c26/CY9n7HghzVay4MpBkrMdCyIgSl6eW/7gk9mc/g="></div>
    <fieldset id="upload">
      <input id="user_avatar" name="user[avatar]" type="file">
      <p class="info">JPG, GIF or PNG. Max size of 700K</p>
    </fieldset>

    <div id="add-btn">
      <input class="form-sub" name="commit" type="submit" value="Upload Now" onclick="file_upload();">
      <input alt="Cancel" class="form-btn" id="cancel-avatar" name="commit" style="display: inline;" type="submit" value="Cancel" onclick="cancel();">
    </div>
</form></div>
</div>
